/*
 * This file is part of ksPlatform
 * 
 * Copyright (c) 2014-2015
 * 
 * This file is dual-licensed under the AGPLv3 (http://www.gnu.org/licenses/agpl-3.0.html) 
 * 
 * If you are unsure which license is appropriate for your use, please contact the sales department
 * at 97041437@qq.com.
 */
package cn.com.ksplatform.modules.system.auth.cachemanager.service;

import java.util.HashSet;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import cn.com.ksplatform.modules.system.auth.buttonmanager.model.PlatButton;
import cn.com.ksplatform.modules.system.auth.menumanager.model.PlatMenu;
import cn.com.ksplatform.modules.system.auth.resourcemanager.model.PlatResource;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

/**
 * ClassName:SystemCacheService.java <br/>
 * Date: 2015年12月8日 <br/>
 * 
 * @author Mr.Xiao
 * @version 1.0
 */
@Component("system.modules.system.SystemCacheService")
public class SystemCacheService {
	@Autowired
	@Qualifier("cacheManagerFactory")
	CacheManager cacheManager;
	
	Cache cache;
	
	void initCache(){
		if(cache == null){
			cache = cacheManager.getCache("RoleDataCache");
		}
	}
	
	public void addMenuToCache(String roleId,PlatMenu menu){
		initCache();
		String key = roleId+"[menus]";
		Element e = cache.get(key);
		if(e == null){
			Set<String> menus = new HashSet<String>();
			menus.add(menu.getMenuId());
			e = new Element(key,menus);
			cache.put(e);
		}else{
			Set<String> menus = (Set<String>) e.getObjectValue();
			menus.add(menu.getMenuId());
		}
	}

	public void removeMenuToCache(String roleId, PlatMenu menu) {
		initCache();
		Element e = cache.get(roleId+"[menus]");
		if(e != null){
			Set<String> menus = (Set<String>) e.getObjectValue();
			menus.remove(menu.getMenuId());
		}
	}

	public void addButtonToCache(String roleId, PlatButton button) {
		initCache();
		String key = roleId+"[btns]";
		Element e = cache.get(key);
		if(e == null){
			Set<String> buttons = new HashSet<String>();
			buttons.add(button.getBtnId());
			e = new Element(key,buttons);
			cache.put(e);
		}else{
			Set<String> buttons = (Set<String>) e.getObjectValue();
			buttons.add(button.getBtnId());
		}
	}

	public void removeButtonToCache(String roleId, PlatButton button) {
		initCache();
		String key = roleId+"[btns]";
		Element e = cache.get(key);
		if(e != null){
			Set<String> buttons = (Set<String>) e.getObjectValue();
			buttons.remove(button.getBtnId());
		}
	}
	
	public void addResToCache(String roleId,PlatResource resource){
		initCache();
		String key = roleId+"[res]";
		Element e = cache.get(key);
		if(e == null){
			Set<String> resources = new HashSet<String>();
			resources.add(resource.getResourceid());
			e = new Element(key,resources);
			cache.put(e);
		}else{
			Set<String> resources = (Set<String>) e.getObjectValue();
			resources.add(resource.getResourceid());
		}
	}
	
	public void removeResToCache(String roleId,PlatResource resource){
		initCache();
		Element e = cache.get(roleId+"[res]");
		if(e != null){
			Set<String> resources = (Set<String>) e.getObjectValue();
			resources.remove(resource.getResourceid());
		}
	}
}
